﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using Domain.UnitOfWork;
using Logic.Services;
using Mestedur.Models;
using Mestedur.ViewModels;
using Services;
using Domain.Entities;
using Domain.Enums;
using Mestedur.Models.Membership;


namespace Mestedur.Controllers
{


    public class EducationController : Controller
    {
        readonly IUnitOfWorkFactory unitOfWorkFactory;
        readonly IUserService userService;
        readonly IQuizService quizService;
        readonly IDatabaseService databaseService;
        public EducationController(IUnitOfWorkFactory unitOfWorkFactory, IUserService userService, IQuizService quizService, IDatabaseService databaseService)
        {
            this.unitOfWorkFactory = unitOfWorkFactory;
            this.userService = userService;
            this.quizService = quizService;
            this.databaseService = databaseService;
        }

        [Authorize]
        public ActionResult GetChapter(int id)
        {
            using (unitOfWorkFactory.Create())
            {
                var Chapter = databaseService.Chapters.Get(id);
                var user = userService.GetUserByName(User.Identity.Name);
                if (quizService.IfThereIsUncompletedTestForUser(user, Chapter))
                {
                    //вернуть оповещение
                    return View("DontCheat");
                }
                else
                {
                    ChapterViewModel viewModel = ViewModelConverter.ConvertChapterToChapterViewModel(Chapter);
                    return View(viewModel);
                }
            }
        }

        
    }
}